<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>管理员列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片区域 -->
    <el-card>
      <!-- 搜索与添加区域 -->
      <el-row :gutter="20">
        <el-col :span="8"
          ><el-input placeholder="请输入用户名" v-model="selectMsg.msg">
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="Select()"
            ></el-button> </el-input></el-col
        ><el-col :span="4">
          <el-button type="primary" @click="addDialogvisible = true"
            >添加用户</el-button
          >
        </el-col>
      </el-row>
      <!-- 表格区域 -->
      <el-table :data="adminlist" stripe border>
        <el-table-column prop="id" label="id" width="50px"> </el-table-column>
        <el-table-column prop="adminName" label="用户名"> </el-table-column>
        <el-table-column prop="adminPwd" label="密码"> </el-table-column>
        <el-table-column prop="createdTime" label="创建时间">
          <template slot-scope="scope">
            {{ scope.row.createdTime | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column prop="updatedTime" label="更新时间">
          <template slot-scope="scope">
            {{ scope.row.updatedTime | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column prop="remark" label="备注"> </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="this.pager.currentPage"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="this.pager.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="this.pager.total"
      >
      </el-pagination>
    </el-card>
    <!-- 添加管理员对话框 -->
    <el-dialog
      title="添加管理员"
      :visible.sync="addDialogvisible"
      width="50%"
      @close="addDialogclosed"
    >
      <!-- 内容主体区域 -->
      <el-form
        :model="addForm"
        :rules="addFormRules"
        ref="addFormRef"
        label-width="70px"
      >
        <!-- 管理员名 -->
        <el-form-item label="用户名" prop="adminName">
          <el-input v-model="addForm.adminName"></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item label="密码" prop="adminPwd">
          <el-input v-model="addForm.adminPwd"></el-input>
        </el-form-item>
        <!-- 备注 -->
        <el-form-item label="备注" prop="remark">
          <el-input v-model="addForm.remark"></el-input>
        </el-form-item>
      </el-form>
      <!-- 底部按钮区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogvisible = false">取 消</el-button>
        <el-button type="primary" @click="AddAdmin()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  mounted () {
    this.getAdminList(this.pager)
  },
  data () {
    return {
      // 搜索框内容
      selectMsg: {
        msg: '',
        currentPage: '',
        pageSize: '',
        total: ''
      },
      // 要操作的行
      currentIndex: 0,
      // 管理员列表
      adminlist: [],
      // 分页用到的数据
      pager: {
        currentPage: 1,
        pageSize: 5,
        total: 0
      },
      // 控制添加管理员对话框的显示和隐藏，默认为false
      addDialogvisible: false,
      // 添加管理员的表单数据绑定
      addForm: {
        adminName: '',
        adminPwd: '',
        remark: ''
      },
      // 添加管理员表单验证规则
      addFormRules: {
        adminName: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 8, message: '长度在 3 到 8 个字符', trigger: 'blur' }
        ],
        adminPwd: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 5, max: 15, message: '长度在 5 到 15 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 管理员名称查找
    Select () {
      if (this.selectMsg.msg) {
        this.SelectAdmin(this.selectMsg)
      } else {
        this.getAdminList(this.pager)
      }
    },
    // 查找方法
    async SelectAdmin (params) {
      params.currentPage = this.pager.currentPage
      params.pageSize = this.pager.pageSize
      params.total = this.pager.total
      const { data: res } = await this.$http.get('admin/select', { params: params })
      if (res.data) {
        this.adminlist = res.data
        this.pager.total = res.count
      }
    },
    // 获取管理员列表
    async getAdminList (params) {
      const { data: res } = await this.$http.get('/admin', { params: params })
      console.log(res)
      this.adminlist = res.data
      this.pager.total = res.count
    },
    // 监听添加用户对话框关闭的事件
    addDialogclosed () {
      this.$refs.addFormRef.resetFields()
    },
    handleSizeChange (newSize) {
      // pagesize改变触发
      this.pager.pageSize = newSize
      this.getAdminList(this.pager)
    },
    handleCurrentChange (newPage) {
      // 页码改变触发
      this.pager.currentPage = newPage
      this.getAdminList(this.pager)
    },
    async AddAdmin () {
      const { data: res } = await this.$http.post('admin', this.addForm)
      if (res.data) {
        this.addDialogvisible = false
        this.getAdminList(this.pager)
      } else {
        this.$message.error(res.msg)
      }
    }
  }
}

</script>

<style scoped>
</style>
